package net.huashitong.Project.util;

import java.util.HashMap;
import java.util.Map;

import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.node.entity.NodeNewtemplet;
import net.huashitong.node.filter.NodeNewtempletFilter;
import net.huashitong.node.service.NodeNewtempletService;
import net.huashitong.oversee.entity.Coordinate;
import net.huashitong.oversee.entity.SupervisionTask;
import net.huashitong.oversee.filter.CoordinateFilter;
import org.springframework.beans.factory.annotation.Autowired;

import com.sirdc.modules.sys.service.SysTableService;
import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.model.Result;
import com.yunpian.sdk.model.SmsSingleSend;

import net.huashitong.user.entity.Message;
import net.huashitong.user.service.MessageService;
import org.springframework.stereotype.Component;

@Component
public class MessageUtil {

    @Autowired
    private MessageService ms;
    @Autowired
    private NodeNewtempletService nodeNewtempletService;


    private final static String apiKey = "b3232c91e3336d52ae80b73bb0a1cb912";       //关闭
//    private final static String apiKey = "b3232c91e3336d52ae80b73bb0a1cb91";

    /**
     * 通知节点任务开始
     *
     * @param user        //姓名
     * @param phone       //电话号码
     * @param projectName //项目名称
     * @param nodeId      //节点id
     * @return
     */
    public Map<String, Object> taskMessage(String user, String phone, String projectName, String nodeId) {
        //根据nodeId查询小节点nodeId
        NodeNewtempletFilter filter = new NodeNewtempletFilter();
        filter.setNodeId(nodeId);
        NodeNewtemplet t = nodeNewtempletService.queryNodeNewTempletByNode(filter);
        String smallNodeName = t.getNodeName();
        //根据nodeId查询大节点nodeId
        String[] bigNodeId = nodeId.split("\\.");
        filter.setNodeId(bigNodeId[0]);
        NodeNewtemplet t1 = nodeNewtempletService.queryNodeNewTempletByNode(filter);
        String bigNodeName = t1.getNodeName();


        Map<String, Object> map = new HashMap<>();
        YunpianClient clnt = new YunpianClient(apiKey).init();
        //修改账户信息API
        Map<String, String> param = clnt.newParam(2);
        param.put(YunpianClient.MOBILE, phone);
        //System.out.println("电话号码="+nextNode.getResUserPhone());
        param.put(YunpianClient.TEXT, "【重大项目节点管理】" + user + "，您好！您的" + projectName + "第" + nodeId + "节点\"" + bigNodeName + "\"" + smallNodeName + "任务已开始，请及时登录系统查看。系统地址：https://www.fzzhengxinyun.com ，帐号为个人手机号，初始密码123456");
        Result<SmsSingleSend> r = clnt.sms().single_send(param);
        System.out.println(r.getMsg());
        //获取返回结果，返回码:r.getCode(),返回码描述:r.getMsg(),API结果:r.getData(),其他说明:r.getDetail(),调用异常:r.getThrowable()
        //账户:clnt.user().* 签名:clnt.sign().* 模版:clnt.tpl().* 短信:clnt.sms().* 语音:clnt.voice().* 流量:clnt.flow().* 隐私通话:clnt.call().*
        //最后释放client
        clnt.close();
        map.put("code", r.getCode());
        map.put("msg", r.getMsg());
        map.put("data", r.getData());
        map.put("text", param.get("text"));
        return map;
    }


    /**
     * 审核回退，通知节点任务开始
     *
     * @param user        //姓名
     * @param phone       //电话号码
     * @param projectName //项目名称
     * @param nodeId      //节点id
     * @return
     */
    public Map<String, Object> taskBackMessage(String user, String phone, String projectName, String nodeId) {
        //根据nodeId查询小节点nodeId
        NodeNewtempletFilter filter = new NodeNewtempletFilter();
        filter.setNodeId(nodeId);
        NodeNewtemplet t = nodeNewtempletService.queryNodeNewTempletByNode(filter);
        String smallNodeName = t.getNodeName();
        //根据nodeId查询大节点nodeId
        String[] bigNodeId = nodeId.split("\\.");
        filter.setNodeId(bigNodeId[0]);
        NodeNewtemplet t1 = nodeNewtempletService.queryNodeNewTempletByNode(filter);
        String bigNodeName = t1.getNodeName();

        Map<String, Object> map = new HashMap<>();
        YunpianClient clnt = new YunpianClient(apiKey).init();
        //修改账户信息API
        Map<String, String> param = clnt.newParam(2);
        param.put(YunpianClient.MOBILE, phone);
        //System.out.println("电话号码="+nextNode.getResUserPhone());
        param.put(YunpianClient.TEXT, "【重大项目节点管理】" + user + "，您好！您的" + projectName + "第" + nodeId + "节点\"" + bigNodeName + "\"" + smallNodeName + "任务被审批部门回退，现在重新开始，请及时登录系统查看。系统地址：https://www.fzzhengxinyun.com ，帐号为个人手机号，初始密码123456");
        Result<SmsSingleSend> r = clnt.sms().single_send(param);
        System.out.println(r.getMsg());
        //获取返回结果，返回码:r.getCode(),返回码描述:r.getMsg(),API结果:r.getData(),其他说明:r.getDetail(),调用异常:r.getThrowable()
        //账户:clnt.user().* 签名:clnt.sign().* 模版:clnt.tpl().* 短信:clnt.sms().* 语音:clnt.voice().* 流量:clnt.flow().* 隐私通话:clnt.call().*
        //最后释放client
        clnt.close();
        map.put("code", r.getCode());
        map.put("msg", r.getMsg());
        map.put("data", r.getData());
        map.put("text", param.get("text"));
        return map;
    }

    /**
     * 通知确定节点操作通知
     *
     * @param user        //姓名
     * @param phone       //电话号码
     * @param projectName //项目名称
     * @param nodeId      //节点id
     * @return
     */
    public Map<String, Object> confirmTaskMessage(String user, String phone, String projectName, String nodeId) {
        //根据nodeId查询小节点nodeId
        NodeNewtempletFilter filter = new NodeNewtempletFilter();
        filter.setNodeId(nodeId);
        NodeNewtemplet t = nodeNewtempletService.queryNodeNewTempletByNode(filter);
        String smallNodeName = t.getNodeName();
        //根据nodeId查询大节点nodeId
        String[] bigNodeId = nodeId.split("\\.");
        filter.setNodeId(bigNodeId[0]);
        NodeNewtemplet t1 = nodeNewtempletService.queryNodeNewTempletByNode(filter);
        String bigNodeName = t1.getNodeName();

        Map<String, Object> map = new HashMap<String, Object>();
        YunpianClient clnt = new YunpianClient(apiKey).init();
        //修改账户信息API
        Map<String, String> param = clnt.newParam(2);
        param.put(YunpianClient.MOBILE, phone);
        //System.out.println("电话号码="+nextNode.getResUserPhone());
        param.put(YunpianClient.TEXT, "【重大项目节点管理】" + user + ",您好！您的" + projectName + "第" + nodeId + "节点\"" + bigNodeName + "\"" + smallNodeName + "信息等你确认，请及时登录系统查看操作。系统地址：https://www.fzzhengxinyun.com ，帐号为个人手机号，初始密码123456");
        Result<SmsSingleSend> r = clnt.sms().single_send(param);
        System.out.println(r.getMsg());
        //获取返回结果，返回码:r.getCode(),返回码描述:r.getMsg(),API结果:r.getData(),其他说明:r.getDetail(),调用异常:r.getThrowable()
        //账户:clnt.user().* 签名:clnt.sign().* 模版:clnt.tpl().* 短信:clnt.sms().* 语音:clnt.voice().* 流量:clnt.flow().* 隐私通话:clnt.call().*
        //最后释放client
        clnt.close();
        map.put("code", r.getCode());
        map.put("msg", r.getMsg());
        map.put("data", r.getData());
        map.put("text", param.get("text"));
        return map;
    }

    /**
     * 预警短信通知
     *
     * @param user        //姓名
     * @param phone       //电话号码
     * @param projectName //项目名称
     * @param nodeId      //节点id
     * @return
     */
    public Map<String, Object> earlyWarningMessage(String user, String phone, String projectName, String nodeId, String nodeName, String deptName) {
        Map<String, Object> map = new HashMap<>();

        //根据nodeId查询小节点nodeId
        NodeNewtempletFilter filter = new NodeNewtempletFilter();
        filter.setNodeId(nodeId);
        NodeNewtemplet t = nodeNewtempletService.queryNodeNewTempletByNode(filter);
        String smallNodeName = t.getNodeName();
        //根据nodeId查询大节点nodeId
        String[] bigNodeId = nodeId.split("\\.");
        filter.setNodeId(bigNodeId[0]);
        NodeNewtemplet t1 = nodeNewtempletService.queryNodeNewTempletByNode(filter);
        String bigNodeName = t1.getNodeName();

        YunpianClient clnt = new YunpianClient(apiKey).init();
        //修改账户信息API
        Map<String, String> param = clnt.newParam(2);
        param.put(YunpianClient.MOBILE, phone);
        //System.out.println("电话号码="+nextNode.getResUserPhone());
        param.put(YunpianClient.TEXT,
                "【重大项目节点管理】" + user + ",您好！您的" + projectName + "下" + nodeId + "节点\"" + bigNodeName + "\"" + nodeName + "任务已超期,责任单位：" + deptName + ",请登录系统抓紧操作。系统地址：https://www.fzzhengxinyun.com ，帐号为个人手机号，初始密码123456");
        Result<SmsSingleSend> r = clnt.sms().single_send(param);
        System.out.println(r.getMsg());
        //获取返回结果，返回码:r.getCode(),返回码描述:r.getMsg(),API结果:r.getData(),其他说明:r.getDetail(),调用异常:r.getThrowable()
        //账户:clnt.user().* 签名:clnt.sign().* 模版:clnt.tpl().* 短信:clnt.sms().* 语音:clnt.voice().* 流量:clnt.flow().* 隐私通话:clnt.call().*
        //最后释放client
        clnt.close();
        map.put("code", r.getCode());
        map.put("msg", r.getMsg());
        map.put("data", r.getData());
        map.put("text", param.get("text"));
    	/*if(r.getCode()==0){
    		Message m=new Message();
    		m.setSysId("1");
    		m.setRecipients(user);
    	    m.setText(param.get("text"));
    	    m.setType("2");
    	  //  ms.save(m);
    	}*/
        return map;
    }

    public Map<String, Object> superviseWarningMessage(String user, String phone, String projectName, String nodeId, String nodeName, String deptName) {

        //根据nodeId查询小节点nodeId
        NodeNewtempletFilter filter = new NodeNewtempletFilter();
        filter.setNodeId(nodeId);
        NodeNewtemplet t = nodeNewtempletService.queryNodeNewTempletByNode(filter);
        String smallNodeName = t.getNodeName();
        //根据nodeId查询大节点nodeId
        String[] bigNodeId = nodeId.split("\\.");
        filter.setNodeId(bigNodeId[0]);
        NodeNewtemplet t1 = nodeNewtempletService.queryNodeNewTempletByNode(filter);
        String bigNodeName = t1.getNodeName();

        Map<String, Object> map = new HashMap<String, Object>();
        YunpianClient clnt = new YunpianClient(apiKey).init();
        //修改账户信息API
        Map<String, String> param = clnt.newParam(2);
        param.put(YunpianClient.MOBILE, phone);
        param.put(YunpianClient.TEXT,
                "【重大项目节点管理】" + user + ",您好！" + projectName + "第" + nodeId + "节点\"" + bigNodeName + "\"" + nodeName + "已超期,责任单位：" + deptName + ",请督促相关人员抓紧办理。系统地址：https://www.fzzhengxinyun.com ，帐号为个人手机号，初始密码123456");
        Result<SmsSingleSend> r = clnt.sms().single_send(param);
        System.out.println(r.getMsg());
        clnt.close();
        map.put("code", r.getCode());
        map.put("msg", r.getMsg());
        map.put("data", r.getData());
        map.put("text", param.get("text"));
        return map;
    }

    public Map<String, Object> remindMessage(String user, String phone, String projectName, String nodeId, String nodeName) {
        Map<String, Object> map = new HashMap<String, Object>();

        //根据nodeId查询小节点nodeId
        NodeNewtempletFilter filter = new NodeNewtempletFilter();
        filter.setNodeId(nodeId);
        NodeNewtemplet t = nodeNewtempletService.queryNodeNewTempletByNode(filter);
        String smallNodeName = t.getNodeName();
        //根据nodeId查询大节点nodeId
        String[] bigNodeId = nodeId.split("\\.");
        filter.setNodeId(bigNodeId[0]);
        NodeNewtemplet t1 = nodeNewtempletService.queryNodeNewTempletByNode(filter);
        String bigNodeName = t1.getNodeName();

        YunpianClient clnt = new YunpianClient(apiKey).init();
        //修改账户信息API
        Map<String, String> param = clnt.newParam(2);
        param.put(YunpianClient.MOBILE, phone);
        param.put(YunpianClient.TEXT, "【重大项目节点管理】" + user + ",您好！" + projectName + "项目第" + nodeId + "节点\"" + bigNodeName + "\"" + nodeName + "任务即将到期，请及时登录系统操作。系统地址：https://www.fzzhengxinyun.com ，帐号为个人手机号，初始密码123456");
        Result<SmsSingleSend> r = clnt.sms().single_send(param);
        System.out.println(r.getMsg());
        clnt.close();
        map.put("code", r.getCode());
        map.put("msg", r.getMsg());
        map.put("data", r.getData());
        map.put("text", param.get("text"));
        return map;
    }


    /*public static void main(String[] args) {
        MessageUtil mu = new MessageUtil();
        SupervisionTask task = new SupervisionTask();
        task.setPhones("18350300651,11111111");
        task.setTaskName("ddd督查任务名dddd");
        task.setProjectName("ddddddafas");
        task.setInspectors("1234");
        mu.remindToDoTaskToSupervisionMessage(task);

    }*/


    //提醒督查组办结短信
    public void remindToDoTaskToSupervisionMessage(SupervisionTask task) {
        String phones = task.getPhones();
        if (StringUtils.isNotBlank(phones)) {
            String[] ps = phones.split(",");
            for (String phone : ps) {
                YunpianClient clnt = new YunpianClient(apiKey).init();
                //修改账户信息API
                Map<String, String> param = clnt.newParam(2);
                param.put(YunpianClient.MOBILE, phone);
                param.put(YunpianClient.TEXT, "【重大项目节点管理】,温馨提醒:您负责的督查任务:" + task.getTaskName() +
                        ",相关责任单位已经完成!请您尽快登陆系统进行审核办结操作。系统地址：https://www.fzzhengxinyun.com ，帐号为个人手机号，初始密码123456");
                Result<SmsSingleSend> r = clnt.sms().single_send(param);
                System.out.println(r.getMsg());
                clnt.close();
                if (r.getCode() == 0) {
                    Message m = new Message();
                    m.setRecipients(task.getInspectors());
                    m.setText(param.get("text"));
                    //提醒办结
                    m.setType("4");
                    m.setPhone(phone);
                    m.setMsg(r.getMsg());
                    m.setProjectName(task.getProjectName());
                    m.setResDept(task.getResDept());
                    m.setBranchType("4");//设置督查组
                    ms.save(m);
                }
            }
        }
    }

    /**
     * 发送攻坚短信给分管领导
     *
     * @param coordinate
     */
    public void sendTackMessageToFGLeader(Coordinate coordinate) {
        String phone = coordinate.getfGcountyLeaderPhone();
        if (StringUtils.isNotBlank(phone)) {
            YunpianClient clnt = new YunpianClient(apiKey).init();
            //修改账户信息API
            Map<String, String> param = clnt.newParam(2);
            param.put(YunpianClient.MOBILE, phone);
            param.put(YunpianClient.TEXT, "【重大项目节点管理】,您好:（" + coordinate.getBaseInfo() +
                    "）,已转批成攻坚任务!请您尽快推进节点进度的开展。系统地址：https://www.fzzhengxinyun.com ，帐号为个人手机号，初始密码123456");
            Result<SmsSingleSend> r = clnt.sms().single_send(param);
            System.out.println(r.getMsg());
            clnt.close();
            if (r.getCode() == 0) {
                Message m = new Message();
                m.setRecipients(coordinate.getfGcountyLeader());
                m.setText(param.get("text"));
                //提醒分管领导项目进入攻坚
                m.setType("5");
                m.setPhone(phone);
                m.setMsg(r.getMsg());
                m.setProjectName(coordinate.getProjectName());
                m.setResDept(coordinate.getResDept());
                m.setBranchType("FG");//设置督查组
                ms.save(m);
            }
        }
    }


}
